#include "matrix_float.h"


matrix_float::matrix_float()
{
	mptr = nullptr;
	r_size = 0;
	c_size = 0;
}

matrix_float::matrix_float(int r, int c)
{
	create_matrix(r, c);
}


matrix_float::matrix_float(const matrix_float &original)
{
	create_matrix(original.r_size, original.c_size);
	for (int i = 0; i < this->r_size; i++)
	{
		for (int j = 0; j < this->c_size; j++)
		{
			this->mptr[i][j] = original.mptr[i][j];
		}
	}
}

matrix_float::~matrix_float()
{
	if (r_size != 0 || c_size != 0)
		delete_matrix();
}

void matrix_float::create_matrix(int r, int c)
{
	r_size = r;
	c_size = c;
	mptr = new float*[r_size];
	for (int i = 0; i < r_size; i++)
		mptr[i] = new float[c_size];
}

void matrix_float::delete_matrix()
{
	for (int i = 0; i < r_size; i++){
		delete[] mptr[i];
	}
	delete[] mptr;
	r_size = 0;
	c_size = 0;
}

void matrix_float::set(float value, int r, int c)
{
	if (r >= 0 && r < r_size && c >= 0 && c<c_size){
		mptr[r][c] = value;
	}
	else
	{
		cout << "error, invalid row and/or column" << endl;
	}
}

void matrix_float::read_from_keyboard()
{
	cout << "ENTER VALUES FOR ALL ELEMENTS IN THE MATRIX" << endl;
	for (int r = 0; r < r_size; r++){
		for (int c = 0; c < c_size; c++){
			cout << "Value [" << r << ", " << c <<"]: ";
			cin >> mptr[r][c];
		}
	}
	cout << "Done entering values..." << endl;
}

void matrix_float::print()
{
	for (int r = 0; r < r_size; r++){
		cout << "[";
		for (int c = 0; c < c_size; c++){
			cout << mptr[r][c];
			if (c == c_size - 1){
				cout << "]" << endl;
			}
			else
			{
				cout << ", ";
			}
		}
	}
}

matrix_float & matrix_float::operator=(matrix_float &rop)
{
	if (r_size != 0 || c_size != 0)
		delete_matrix();
	create_matrix(rop.r_size, rop.c_size);
	for (int i = 0; i < this->r_size; i++)
	{
		for (int j = 0; j < this->c_size; j++)
		{
			this->mptr[i][j] = rop.mptr[i][j];
		}
	}
	return *this;
}